﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Web.fanliAdmin.admin
{
    public partial class AdminList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            switch (cmd)
            {
                case "add": initAddForm(); break;
                case "edit": initEditFrom(); break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            showTheRightRegion(cmd);
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            fanli.DAL.fanli_adminUsers dal = new fanli.DAL.fanli_adminUsers();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion          

            #region 分页控件绑定
            int pageSize = 30; //分页每页记录个数
            int totalRecourd = 0; //总记录个数
            int currentPage = 1; //当前页码
            try { currentPage = int.Parse(Request.QueryString["p"]); }
            catch { }

            totalRecourd = dal.GetCount(strWhere, paraList);
            dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
            if (dset != null && dset.Tables[0].Rows.Count > 0)
            {
                rptInfoList.DataSource = dset;
                rptInfoList.DataBind();
                litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
            }
            else
            {
                litPromptMessage.Text = "没有查询到相关记录";
            }
            #endregion
            #region 单页数据绑定
            //strWhere += " " + strOrderBy;
            //dset = dal.GetDataset(strWhere, paraList, dataFields);
            //if (dset.Tables[0].Rows.Count > 0)
            //{
            //    rptInfoList.DataSource = dset;
            //    rptInfoList.DataBind();
            //}
            //else
            //{
            //    litPromptMessage.Text = "没有查询到相关记录";
            //} 
            #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = true;
            int varId;	//id
            string varUsername;	//用户名
            //string varPassword;	//密码
            string varRealname;	//真实姓名
            string varLevel;//角色
            string varQq;	//qq
            string varEmail;	//邮箱
            string varTelephone;	//电话
            //DateTime varLastloginTime ;	//最后一次登陆时间
            //string varLastloginip;	//最后一次登陆ip
            //int varLoginnum ;	//登陆次数
            int varStatus;	//状态
            string varExt1;	//扩展1
            string varExt2;	//扩展2

            try
            {
                varId = int.Parse(txtId.Text.Trim());	//id
                varUsername = txtUsername.Text.Trim();	//用户名
                //varPassword =txtPassword.Text.Trim();	//密码
                varRealname = txtRealname.Text.Trim();	//真实姓名
                varLevel = this.rblRole.SelectedValue;//角色
                varQq = txtQq.Text.Trim();	//qq
                varEmail = txtEmail.Text.Trim();	//邮箱
                varTelephone = txtTelephone.Text.Trim();	//电话
                //varLastloginTime =DateTime.Parse(txtLastloginTime.Text.Trim());	//最后一次登陆时间
                //varLastloginip =txtLastloginip.Text.Trim();	//最后一次登陆ip
                //varLoginnum =int.Parse(txtLoginnum.Text.Trim());	//登陆次数
                varStatus = int.Parse(this.ddlStatus.SelectedValue);	//状态
                varExt1 = txtExt1.Text.Trim();	//扩展1
                varExt2 = txtExt2.Text.Trim();	//扩展2
                if (varUsername=="")
                {
                    LIB.MessageBox.Show(this, "用户名不能为空");
                    return;
                }
            }
            catch
            {
                LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }
            if (varId != 0) isAdd = false;
            #endregion

            fanli.DAL.fanli_adminUsers dal = new fanli.DAL.fanli_adminUsers();
            #region 赋值model
            fanli.Model.fanli_adminUsers model = new fanli.Model.fanli_adminUsers();
            if (!isAdd) model = dal.GetModel(varId);

            model.id = varId;	//id
            model.username = varUsername;	//用户名
            //model.password = varPassword;	//密码
            model.realname = varRealname;	//真实姓名
            model.level = varLevel;	//角色
            model.qq = varQq;	//qq
            model.email = varEmail;	//邮箱
            model.telephone = varTelephone;	//电话
            //model.lastloginTime = varLastloginTime;	//最后一次登陆时间
            //model.lastloginip = varLastloginip;	//最后一次登陆ip
            //model.loginnum = varLoginnum;	//登陆次数
            model.status = varStatus;	//状态
            model.ext1 = varExt1;	//扩展1
            model.ext2 = varExt2;	//扩展2
            #endregion

            #region 执行操作
            if (isAdd)
            {
                if (GetExistName(this.txtUsername.Text.Trim()))
                {
                    LIB.MessageBox.Show(this,"用户名重复");
                    return;
                }
                model.password = LIB.MD5Pwd.EPwd("123");
                if (dal.Add(model) > 0)
                {

                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {

                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            }
            else
            {
                if (dal.Update(model) > 0)
                {

                    LIB.JsWrite.AlertAndGo(this, "修改成功", "?cmd=list");
                }
                else
                {

                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            }
            #endregion
        }

        //判断用户名是否重复
        private bool GetExistName(string name)
        {
            bool result = false;
            var sql = "username=@username";
            List<SqlParameter> para = new List<SqlParameter>();
            para.Add(new SqlParameter("@username", name));
            fanli.DAL.fanli_adminUsers bll = new fanli.DAL.fanli_adminUsers();
            List<fanli.Model.fanli_adminUsers> list = bll.GetModelList(sql, para, "");
            if (list.Count > 0)
            { result = true; }
            return result;
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            fanli.DAL.fanli_adminUsers dal = new fanli.DAL.fanli_adminUsers();
            if (dal.Delete(id) > 0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {

                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            }
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtUsername.Text = "";
                //txtPassword.Text = "";
                txtRealname.Text = "";
                //txtLevel.Text = "";
                txtQq.Text = "";
                txtEmail.Text = "";
                txtTelephone.Text = "";
                txtExt1.Text = "";
                txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            this.txtUsername.Enabled = false;
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_adminUsers model = new fanli.Model.fanli_adminUsers();
                fanli.DAL.fanli_adminUsers dal = new fanli.DAL.fanli_adminUsers();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtUsername.Text = model.username.ToString();
                //txtPassword.Text = model.password.ToString();
                txtRealname.Text = model.realname.ToString();
                this.rblRole.SelectedValue= model.level.ToString();
                txtQq.Text = model.qq.ToString();
                txtEmail.Text = model.email.ToString();
                txtTelephone.Text = model.telephone.ToString();

                this.ddlStatus.SelectedValue = model.status.ToString();
                txtExt1.Text = model.ext1.ToString();
                txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_adminUsers model = new fanli.Model.fanli_adminUsers();
                fanli.DAL.fanli_adminUsers dal = new fanli.DAL.fanli_adminUsers();
                model = dal.GetModel(id);

                lblUsername.Text = model.username.ToString();
                //lblPassword.Text = model.password.ToString();
                lblRealname.Text = model.realname.ToString();
                lblLevel.Text = model.level.ToString();
                lblQq.Text = model.qq.ToString();
                lblEmail.Text = model.email.ToString();
                lblTelephone.Text = model.telephone.ToString();
                lblLastloginTime.Text = model.lastloginTime.ToString();
                lblLastloginip.Text = model.lastloginip.ToString();
                lblLoginnum.Text = model.loginnum.ToString();
                lblStatus.Text = model.status.ToString() == "1" ? "正常" : "冻结";
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion

    }
}